// pkgTask/pages/addEditFee/components/choose-user/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    taskList: {
      type: Array,
      value: [],
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    list: [],
    popup: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    isCheckedTask(task, checkedTasks = []) {
      const vm = this
      let checked = false
      if (!task || !checkedTasks || checkedTasks.length <= 0) {
        return checked
      }
      checkedTasks.forEach(item => {
        if (item.id == task.id) {
          checked = true
        }
      })
      return checked
    },

    show(checkedTasks = []) {
      const vm = this
      let list = []
      if (vm.properties.taskList && vm.properties.taskList.length > 0) {
        vm.properties.taskList.forEach(item => {
          list.push({
            id: item.id,
            type: item.type,
            name: item.name,
            date: item.date,
            _checked: vm.isCheckedTask(item, checkedTasks),
          })
        })
      }
      vm.setData({
        list: list,
        popup: true
      })
    },

    close() {
      this.setData({
        popup: false
      })
    },

    chooseTask(e) {
      const vm = this
      console.log(e)
      let id = parseInt(e.currentTarget.dataset.uid)
      let list = vm.data.list
      list.forEach(item => {
        if (item.id == id) {
          item._checked = !item._checked
        }
      })
      vm.setData({
        list: list
      })

      let checkedList = []
      vm.data.list.forEach(item => {
        if (item._checked) {
          checkedList.push(item)
        }
      })

      vm.triggerEvent('change', checkedList)
    },
  }
})